// pages/coupons/coupons.js
const app = getApp()
var api = require('../../../utils/api.js')
var util = require('../../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
     order_type: 4,
     page: 1,
     limit: 10,
     product:[],
     remarkCol: true, 
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
     this.getOrderLIst();
  },
   changeType: function (e) {
      var types = e.currentTarget.dataset.type;
      this.setData({
         page: 1,
         order_type: types
      })
      this.getOrderLIst();
   },
   // 获取列表
   getOrderLIst: function () {
      var params = new Object()
      var that = this;
      params.page = this.data.page;
      params.limit = this.data.limit;
      params.type = this.data.order_type;

      api.POST({
         params: params,
         url: app.globalData.base_url + 'Mealticket/list',
         success: function (res) {
            wx.hideLoading();
            if (util.isEmpty(res.data.code)) {
               var paging = res.data.data.paging;
               var totalPage = paging.total_page;
               var orderList = res.data.data.list;
               var giftCardNum = res.data.data.giftCardNum;

               if (that.data.page == 1) {
                  var itemData = orderList;
               } else {
                  var itemData = that.data.product.concat(orderList)
               }

               that.setData({
                  total_page: totalPage,
                  product: itemData,
                  giftCardNum: giftCardNum
               })
            }
         },
         fail: function (fail) {
            util.showError("网络错误，请检查网络");
         }
      })
   },

   //加载更多
   loadMore: function () {
      if (this.data.page >= this.data.total_page) {
         return;
      }
      wx.showLoading({
         title: '加载中',
      })
      this.setData({
         page: this.data.page + 1
      });
      this.getOrderLIst();
   },

   //刷新页面
   resetData: function () {
      var that = this
      that.setData({
         page: 1,
         hasMore: true
      });
   },
   //页面下拉刷新
   onPullDownRefresh: function () {
      var that = this;

      setTimeout(function () {
         that.resetData()
      }, 1000)
      wx.stopPullDownRefresh();
   },

   //页面上拉触底
   onReachBottom: function (e) {
      this.loadMore();
   },

   //赠送餐券
   sendCard: util.debounce(function(e){
      var id = e.currentTarget.dataset.id
      wx.navigateTo({
         url: '/play/pages/gift/gift?coupons_selectedId=' + id
      })
      
   }),
   //使用餐券
   useCard: util.debounce(function (e) {
      var id = e.currentTarget.dataset.id
      wx.navigateTo({
         url: '/center/pages/couponsUse/couponsUse?id=' + id
      })
   }),
   giftLink:util.debounce(function(e){
      wx.navigateTo({
         url: '/play/pages/myGift/myGift'
      })
   }),
   remarkCancel: function () {
      this.setData({
         remarkCol: true
      })
   },

   remarkOpen: function () {
      this.setData({
         remarkCol: false
      })
   },
})